package de.klingbeil.hhc.testhelper;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import org.mockito.ArgumentCaptor;

import com.google.inject.Provider;
import com.google.web.bindery.requestfactory.shared.Receiver;

@SuppressWarnings("unchecked")
public final class CaptureUtils {

	public static <T> T capture(Receiver<T> receiver) {
		ArgumentCaptor<T> captor = (ArgumentCaptor<T>) ArgumentCaptor
				.forClass(Object.class);
		verify(receiver).onSuccess(captor.capture());
		return captor.getValue();
	}

	public static <T> Provider<T> mockedProvider(T toProvide) {
		Provider<T> provider = mock(Provider.class);
		when(provider.get()).thenReturn(toProvide);
		return provider;
	}
}
